package service;

import dto.ConfDto;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by cxland on 2017/10/11.
 */
public class ConfigService {
    private static File confFile = null ;
    private static FileInputStream inputStream = null;

    /**
     * 查找配置文件是否存在
     * @param filePath
     * @return
     */
    public  static Boolean findFile(String filePath){
        File file = new File(filePath);
        if(!file.exists()){
            System.out.println("#找不到配置文件");
            return false;
        }
        confFile=file;
        return true;
    }

    /**
     * 获取配置文件中指定的工程路径
     * @return
     * @throws Exception
     */
    public static String projectPath() throws Exception{
        ConfDto dto  = getData();
        return  dto.getProjectPath();
    }

    /**
     * 获取配置文件中指定的数据链接地址
     * @return
     * @throws Exception
     */
    public static Map<String,String> dbConf() throws Exception{
        ConfDto dto  = getData();
        return dto.getDbConf();
    }

    /**
     * 获取配置文件中的允许生成的数据表
     * @return
     * @throws Exception
     */
    public static String[] ruleConf() throws Exception{
       ConfDto dto = getData();
       return dto.getIgnoreTable();
    }


    /**
     * 读取配置文件数据
     * @return
     * @throws Exception
     */
    public static ConfDto getData( ) throws Exception{
        FileInputStream pjIn =  new FileInputStream(confFile);
        //数据转化
        Yaml pjYaml = new Yaml();
        ConfDto dto = pjYaml.loadAs(pjIn,ConfDto.class);
        pjIn.close();
        return dto;
    }

    /**
     * 获取数据表生成的包名
     * @return
     * @throws Exception
     */
    public static Map<String, String> getAllPackageName() throws Exception {
        ConfDto dto = getData();

        return dto.getPackageConf();
    }

    /**
     * 获取指定的数据表的包名
     * @param table
     * @return
     * @throws Exception
     */
    public static String getPackageName(String table) throws Exception {
        Map<String,String>map = getAllPackageName();
        String tableName = table.concat("_package");
        return map.get(tableName);
    }
}
